package com.jbp.job;

import com.jbp.redis.RedisConfigService;
import com.jbp.service.*;
import com.jbp.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.FileWriter;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Random;

/**创建新用户*/

@Component
@Configuration
public class CreateUserJob {
    @Autowired
    private UserCreatorService userCreatorService;

    @Value("${xiaohao.namePrex}")
    private String namePrex;

    @Value("${xiaohao.open}")
    private boolean open;

    @Autowired
    private RedisConfigService redisConfigService;

    @Autowired
    private PropertyService propertyService;


    @Scheduled(fixedDelay = 1000*60*7)  //任务时间
    public void saveUsers(){
        String user_create = propertyService.getV("user_create");
        String user_create_counts = propertyService.getV("user_create_counts");
        if (user_create !=null && user_create.equals("1")){
            System.out.println("resetRedis");
            redisConfigService.removePattern("kchat:*");
            redisConfigService.removePattern("*");

            for (int j = 1; j <= Integer.parseInt(user_create_counts); j++) {
                printUser("-----------第"+j+"组-------------"+"\n", namePrex);
                System.out.println("-----------第"+j+"组-------------");
                String  users = createNewUser();
                printUser(users, namePrex);
            }
        }


        /*for (int z = 1; z <= 5; z++) {
            String fileName=namePrex+"小号"+z;
            for (int j = 1; j <= 2; j++) {
                printUser("-----------第"+j+"组-------------"+"\n", fileName);
                System.out.println("-----------第"+j+"组-------------");
                String  users = createNewUser();
                printUser(users, fileName);
            }
        }*/
    }

    public String createNewUser(){
        if (open) {
            Random rand = new Random();//生成随机数
            String chars = "abcdefghijklmnopkrstuvwxyz";
            String key = "user:new:cache";
            String name = RobNameUtil.getRobName();
            System.out.println("创建账号");
            StringBuilder sb = new StringBuilder();
            String pwd = "";
            for (int i = 0; i < 2; i++) {
                int num = rand.nextInt(chars.length());
                pwd += chars.charAt(num);
                pwd += chars.charAt(num);
            }
            for (int a = 0; a < 2; a++) {
                int k = rand.nextInt(10);
                pwd += k;
                pwd += k;
            }
            String phone = MobileUtil.getMobile();
            boolean flag = userCreatorService.createUser(phone, pwd, name,10);
            if (flag) {
                sb.append(""+phone+"    "+pwd).append("\n");
                System.out.println("账号：" + phone + " 密码:" + pwd + " 名称:" + name);
            }
            /*for (int m = 1; m < 3; m++) {
                String pwd = "";
                for (int i = 0; i < 2; i++) {
                    int num = rand.nextInt(chars.length());
                    pwd += chars.charAt(num);
                    pwd += chars.charAt(num);
                }
                for (int a = 0; a < 2; a++) {
                    int k = rand.nextInt(10);
                    pwd += k;
                    pwd += k;
                }
                String phone = MobileUtil.getMobile();
                boolean flag = userCreatorService.createUser(phone, pwd, name,10);
                if (flag) {
                    sb.append(""+phone+"    "+pwd).append("\n");
                    System.out.println("账号：" + phone + " 密码:" + pwd + " 名称:" + name);
                }
            }*/
            return sb.toString();
        }
        return "";
    }

    /*public String createNewUser(){
        if (open) {
            String chars = "abcdefghijklmnopkrstuvwxyz";
            String key = "user:new:cache";
            String pwd = "";
            Random rand = new Random();//生成随机数
            String name = namePrex + chars.charAt(rand.nextInt(chars.length()));
            for (int i = 0; i < 2; i++) {
                int num = rand.nextInt(chars.length());
                pwd += chars.charAt(num);
                pwd += chars.charAt(num);
            }

            for (int a = 0; a < 2; a++) {
                int k = rand.nextInt(10);
                pwd += k;
                pwd += k;
            }

            System.out.println("创建账号");
            StringBuilder sb = new StringBuilder();
            for (int m = 1; m < 11; m++) {
                String phone = MobileUtil.getMobile();
                boolean flag = userCreatorService.createUser(phone, pwd, name,10);
                if (flag) {
                    sb.append(""+phone+"    "+pwd).append("\n");
                    System.out.println("账号：" + phone + " 密码:" + pwd + " 名称:" + name);
                }
            }
            return sb.toString();
        }
        return "";
    }*/

//    @Scheduled(fixedDelay = 1000*60*60*60)
    public void refreshData(){
        String chars= "abcdefghijklmnopkrstuvwxyz";
        String phone1 = "174";
        String fileName="呱呱小号1";
        String name = "呱呱a";
        int count = 10;
        for (int j = 1; j <= count; j++) {
            String pwd="";
            Random rand=new Random();//生成随机数
            for (int i = 0; i < 2; i++) {
                int num = UUIDUtil.getNonce(25, 0);
                pwd += chars.charAt(num);
                pwd += chars.charAt(num);
            }
            for(int a=0;a<2;a++){
                int k = rand.nextInt(10);
                pwd += k;
                pwd += k;
            }

            String phone2 = phone1;
            for (int i = 0; i < 4; i++) {
                phone2 += rand.nextInt(10);
            }

            System.out.println("-----------第"+j+"组-------------");
            printUser("-----------第"+j+"组-------------"+"\n", fileName);
            for (int m = 1; m < 11; m++) {
                String phone = phone2;
                for (int i = 0; i < 4; i++) {
                    phone += rand.nextInt(10);
                }

                boolean flag = userCreatorService.createUser(phone, pwd, name, 0);
                if (flag) {
                    System.out.println("账号：" + phone + " 密码:" + pwd);
                    printUser(""+phone+"    "+pwd+"\n", fileName);
                }
            }
        }
    }

    static LinkedHashMap<Integer, Double> printUser(String users, String fileName){
        try {
            FileWriter fileWriter = new FileWriter(fileName+".txt", true);
            fileWriter.write(users);
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String chars= "abcdefghijklmnopkrstuvwxyz";
        String phone1 = "189";
        for (int j = 1; j <= 100; j++) {
            String pwd="";
            Random rand=new Random();//生成随机数
            for (int i = 0; i < 2; i++) {
                int num = UUIDUtil.getNonce(25, 0);
                pwd += chars.charAt(num);
                pwd += chars.charAt(num);
            }
            for(int a=0;a<2;a++){
                int k = rand.nextInt(10);
                pwd += k;
                pwd += k;
            }

            String phone2 = phone1;
            for (int i = 0; i < 4; i++) {
                phone2 += rand.nextInt(10);
            }

            System.out.println("-----------第"+j+"组-------------");
            for (int m = 1; m < 11; m++) {
                String phone = phone2;
                for (int i = 0; i < 4; i++) {
                    phone += rand.nextInt(10);
                }


                System.out.println("账号："+phone+" 密码:"+pwd);
            }


        }
    }
}

